package com.jiaming.employee.salary.controller;

import com.jiaming.employee.attendance.bean.vo.DayVO;
import com.jiaming.employee.common.bean.vo.MyPageVO;
import com.jiaming.employee.common.response.ResultCodeEnum;
import com.jiaming.employee.common.response.ResultVO;
import com.jiaming.employee.salary.bean.pojo.Salary;
import com.jiaming.employee.salary.bean.vo.GetSalaryPageVO;
import com.jiaming.employee.salary.bean.vo.UpdateSalaryVO;
import com.jiaming.employee.salary.service.ISalaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author dragon
 * @create 2021-05-14 21:08
 */
@Api(value = "工资API接口文档")
@RestController
@RequestMapping("/salary")
public class SalaryController {

    @Autowired
    ISalaryService salaryService;

    @ApiOperation(value = "新增员工工资",notes = "接口描述")
    @PostMapping("/save_salary")
    public ResultVO saveSalary(@RequestBody @Valid Salary salary) {
        salaryService.saveSalary(salary);
        return new ResultVO(ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "删除员工工资",notes = "接口描述")
    @PostMapping("/remove_salary")
    public ResultVO removeSalary(@RequestParam Integer id) {
        salaryService.removeById(id);
        return new ResultVO(ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "修改员工工资",notes = "接口描述")
    @PostMapping("/update_salary")
    public ResultVO updateSalary(@RequestBody @Valid UpdateSalaryVO updateSalaryVO) {
        Salary salary = new Salary();
        BeanUtils.copyProperties(updateSalaryVO,salary);
        salaryService.updateById(salary);
        return new ResultVO(ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "查询员工工资",notes = "接口描述")
    @PostMapping("/get_salary")
    public ResultVO<MyPageVO<Salary>> getSalary(@RequestBody GetSalaryPageVO getSalaryPageVO) {
        if (getSalaryPageVO.getPageNum() == null || getSalaryPageVO.getPageNum() <= 0) {
            getSalaryPageVO.setPageNum(1);
        }
        if (getSalaryPageVO.getPageSize() == null || getSalaryPageVO.getPageSize() < 5) {
            getSalaryPageVO.setPageSize(5);
        }
        MyPageVO<Salary> salaryMyPageVO = salaryService.getSalary(getSalaryPageVO);
        return new ResultVO<>(ResultCodeEnum.SUCCESS,salaryMyPageVO);
    }
}
